package com.tim.android.widget.listview;

import java.util.List;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;

public abstract class GroupableListAdapter implements ExpandableListAdapter {
	
	private static final String TAG = "GroupableListAdapter";
	
	private List<GroupItem> mData;
	
	private boolean mIsChildSelectable;
	
	private Context mContext;
	
	
	public GroupableListAdapter(Context context, List<GroupItem> data) {
		this.mContext = context;
		this.mData = data;
	}
	
	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {

	}

	@Override
	public int getGroupCount() {
		return mData == null ? 0 : mData.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		List<?> children = null;
		if(mData != null) {
			children = mData.get(groupPosition).getChildren();
		}
		return children == null ? 0 : children.size();
	}
	
	public GroupItem getGroupItem(int groupPosition) {
		return (GroupItem) getGroup(groupPosition);
	}

	@Override
	public Object getGroup(int groupPosition) {
		return mData.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return mData.get(groupPosition).getChildren().get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return groupPosition * 10 + childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, 
			View convertView, ViewGroup parent) {
		Log.e(TAG, "onGetGroupView ..");
		GroupView groupView = null;
		if(convertView == null) {
			 groupView = new GroupView(mContext);
			 convertView = groupView;
		}
		else {
			groupView = (GroupView) convertView;
		}
		groupView.groupTitle.setTextColor(Color.BLACK);
		groupView.groupTitle.setText(getGroupItem(groupPosition).getGroupName());
		return convertView;
	}
	
	@Override
	public abstract View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent);

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return mIsChildSelectable;
	}
	
	public void setChildSelectable(boolean isChildSelectable) {
		this.mIsChildSelectable = isChildSelectable;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEmpty() {
		return mData == null || mData.size() == 0;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {

	}

	@Override
	public void onGroupCollapsed(int groupPosition) {

	}

	@Override
	public long getCombinedChildId(long groupId, long childId) {
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		return 0;
	}
}
